/*sm4密钥
 */  

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <stdint.h>  
#include "gmssl/sm4.h"
//生成密钥

#include "gmssl/rand.h"

// 定义明文最小长度  
#define PLAINTEXT_MIN_SIZE 130  
void duqu(char *filename, char **in, int *len) {

	FILE *file = fopen(filename, "rb");
	if (!file) {
		perror("Failed to open file");
		return;
	}

	fseek(file, 0, SEEK_END);
	*len = ftell(file);
	fseek(file, 0, SEEK_SET);

	*in = (char *)calloc(*len, 1);

	if (!*in) {
		perror("Failed to allocate memory");
		fclose(file);
		return;
	}

	fread(*in, 1, *len, file);
	fclose(file);
}

void xieru(char *filename, uint8_t *out, int len) {

	FILE *file = fopen(filename, "wb");
	if (!file) {
		perror("Failed to open file");
		return;
	}

	fwrite(out, sizeof(uint8_t), len, file);
	fclose(file);
}

int main(int argc, char *argv[]){
	if(argc<3){
	printf("no enough filename\n");
	return 0;
	} 
    // 生成随机密钥  
    uint8_t key[SM4_KEY_SIZE];  
    if (rand_bytes(key, SM4_KEY_SIZE) != 1) {  
        fprintf(stderr, "随机密钥生成失败。\n");  
        return EXIT_FAILURE;  
    }  

    // 生成随机IV  
    uint8_t iv[SM4_BLOCK_SIZE];  
    if (rand_bytes(iv, SM4_BLOCK_SIZE) != 1) {  
        fprintf(stderr, "随机IV生成失败。\n");  
        return EXIT_FAILURE;  
    }  

    // 打印生成的密钥  
    xieru(argv[1], key, SM4_KEY_SIZE);
    
    // 打印生成的IV  
    xieru(argv[2], iv, SM4_BLOCK_SIZE); 

    

    return EXIT_SUCCESS;  
}
